package libnet

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"net"
)

//监听
func Listener() {
	tcpaddr, err := net.ResolveTCPAddr("tcp", "10.222.138.14:7725")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(tcpaddr.IP)
	fmt.Println(tcpaddr.Network())
	fmt.Println(tcpaddr.Port)
	fmt.Println(tcpaddr.String())
	fmt.Println(tcpaddr.Zone)
	listener, err := net.ListenTCP("tcp", tcpaddr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(listener.Addr())
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Fatal(err)
		}
		defer conn.Close()
		r := bufio.NewReader(conn)

		chunks := make([]byte, 1024, 1024)
		buf := make([]byte, 1024)
		for {
			n, err := r.Read(buf)
			if err != nil && err != io.EOF {
				log.Fatal(err)
				panic(err)
			}
			if n == 0 {
				break
			}
			fmt.Println(buf[:n])
			chunks = append(chunks, buf[:n]...)
		}
		fmt.Println(string(chunks))
		fmt.Println(" 结束")
	}

}
